package com.wan.activity

import android.graphics.Bitmap
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.KeyEvent
import android.view.ViewGroup
import android.webkit.*
import com.wan.R
import com.wan.uitls.CommonUtils
import kotlinx.android.synthetic.main.activity_web.*

/**
 * 展示web页面
 * webView 极其容易造成内存泄漏
 */
class WebActivity : AppCompatActivity() {
    private val mThreadName = this.javaClass.name
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_web)

        initView()
    }

    private fun initView() {
        var urlStr = intent.getStringExtra(CommonUtils.Intent_URL)
        var titleStr = intent.getStringExtra(CommonUtils.Intent_Title)



        web_web_wv.loadUrl(urlStr)
        web_web_wv.webChromeClient = webChromeClient
        web_web_wv.webViewClient = webViewClient

        web_web_wv.settings.cacheMode = WebSettings.LOAD_CACHE_ELSE_NETWORK

        //设置是否支持缩放
        web_web_wv.settings.setSupportZoom(true)
        web_web_wv.settings.builtInZoomControls = true
        //是否显示缩放按钮
        web_web_wv.settings.displayZoomControls = false

        //设置网页自适应
        web_web_wv.settings.useWideViewPort=true
        web_web_wv.settings.loadWithOverviewMode=true
    }

    private val webChromeClient = object : WebChromeClient() {
        override fun onProgressChanged(view: WebView?, newProgress: Int) {
            super.onProgressChanged(view, newProgress)
            Log.d(mThreadName, " onProgressChanged: progress .. $newProgress")

        }
    }

    private val webViewClient = object : WebViewClient() {
        override fun onPageFinished(view: WebView?, url: String?) {
            super.onPageFinished(view, url)
            Log.d(mThreadName, "onPageStarted : 加载结束");
        }

        override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
            super.onPageStarted(view, url, favicon)
            Log.d(mThreadName, "onPageStarted : 加载开始");
        }

        override fun onReceivedError(view: WebView?, request: WebResourceRequest?, error: WebResourceError?) {
            super.onReceivedError(view, request, error)

        }

    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        //是否有上一级目录
        if (web_web_wv.canGoBack() and (keyCode == KeyEvent.KEYCODE_BACK)) {
            web_web_wv.goBack()
            return true
        }
        return super.onKeyDown(keyCode, event)
    }

    override fun onDestroy() {
        var viewParent = web_web_wv.parent

        if (viewParent != null) {
            var viewGroup = viewParent as ViewGroup
            viewGroup.removeView(web_web_wv)
        }
        web_web_wv.stopLoading()
        web_web_wv.settings.javaScriptEnabled = false
        web_web_wv.clearHistory()
        web_web_wv.loadUrl("about:blank")
        web_web_wv.removeAllViews()
        web_web_wv.destroy()
        super.onDestroy()
    }


}
